﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using CP.Entities;
using CP.Controllers.GPM;
using DevExpress.XtraEditors;
using CP.Utility;
namespace GPM
{
    public partial class FrmEditModule : BaseEditForm
    {
        public int ModuleID;
        public ModuleController controller = new ModuleController();
        public FrmEditModule()
        {
            InitializeComponent();
        }
        public FrmEditModule(string id, int moduleid)
            : this()
        {
            base.EntityID = id;
            this.ModuleID = moduleid;
        }

        private void SetInfo(Module module)
        {
            module.module_id = Convert.ToInt32(this.txtModuleID.Text);
            module.module_name = this.txtModuleName.Text;
            module.assembly_name = this.txtAssembly.Text;
            module.name_space = this.txtNameSpace.Text;  
            module.remark = this.memoRemark.Text;
            module.db_type = 2;
        }

        /// <summary>
        /// 数据字典加载
        /// </summary>
        private void InitDictItem()
        {

        }

        public override bool CheckInput()
        {

            if (string.IsNullOrEmpty(this.txtModuleName.Text.Trim()))
            {
                ExMessageBox.Show("提示信息：模块名称不能为空！", AppSetter.SoftName);
                return false;
            }

            if (string.IsNullOrEmpty(this.txtAssembly.Text.Trim()))
            {
                ExMessageBox.Show("提示信息：程序集文件名不能为空！", AppSetter.SoftName);
                return false;
            }

            if (string.IsNullOrEmpty(this.txtNameSpace.Text.Trim()))
            {
                ExMessageBox.Show("提示信息：命名空间不能为空！", AppSetter.SoftName);
                return false;
            } 

            return true;
        }

        public override void DisplayData()
        {
            InitDictItem();

            if (!string.IsNullOrEmpty(EntityID))
            {
                Module module = controller.GetModuleByID(this.ModuleID);
                #region 显示用户信息

                this.txtModuleID.Focus();
                if (null != module)
                {
                    this.txtModuleID.Text = module.module_id.ToString();
                    this.txtModuleID.Enabled = false;
                    this.txtModuleName.Text = module.module_name;
                    this.txtAssembly.Text = module.assembly_name;
                    this.txtNameSpace.Text = module.name_space; 
                    this.memoRemark.Text = module.remark; 
                } 
                #endregion
            }
            else
            {
                this.txtModuleID.Focus();
            };
        }

        public override bool SaveAddNew()
        {
            Module module = new Module();
            this.SetInfo(module);
            controller.AddModule(module);
            LogHelper.Log("模块管理", "新增", "", "module:" +
             module.module_id.ToString());
            (this.Owner.ActiveMdiChild as FrmModule).RefreshModule();
            return true;
        }

        public override bool SaveUpdated()
        {
            if (string.IsNullOrEmpty(this.txtModuleID.Text.Trim()))
            {
                ExMessageBox.Show("提示信息：模块ID不能为空！", AppSetter.SoftName);
                return false;
            }

            Module module = new Module();
            this.SetInfo(module);
            controller.EdiModule(module);
            LogHelper.Log("模块管理", "编辑", "", "module:" +
            module.module_id.ToString());
            (this.Owner.ActiveMdiChild as FrmModule).RefreshModule();
            return true;
        }
    }
}
